#!/bin/bash
# default to mingw32 flavor unless $host is msys...
MPFLAVOR=i686-pc-mingw32
case $(uname -s) in
MSYS*) MPFLAVOR=i686-pc-msys;;
esac

set -e

rm -f ${PACKAGE}-${VERSION}-1-mingw32-src.tar.lzma \
  ${PACKAGE}-${VERSION}-1-mingw32-bin.tar.lzma \
  ${PACKAGE}-${VERSION}-1-mingw32-doc.tar.lzma \
  ${PACKAGE}-${VERSION}-1-mingw32-lic.tar.lzma \
  ${PACKAGE}-${VERSION}-1-msys-*-src.tar.lzma \
  ${PACKAGE}-${VERSION}-1-msys-*-bin.tar.lzma \
  ${PACKAGE}-${VERSION}-1-msys-*-doc.tar.lzma \
  ${PACKAGE}-${VERSION}-1-msys-*-lic.tar.lzma

if [ -f ${top_srcdir}/../${PACKAGE}-${VERSION}.tar.xz ]
then
	# running under make distcheck
	# make dist during make distcheck won't work because all files in
	# DISTDIR are read-only
	cp -f ${top_srcdir}/../${PACKAGE}-${VERSION}.tar.xz .
else
	${MAKE} -C ${top_builddir} dist-xz > /dev/null
	cp -f ${top_builddir}/${PACKAGE}-${VERSION}.tar.xz .
fi

case "${MPFLAVOR}" in
*mingw*) EXT=mgwport;;
*msys*)  EXT=msysport;;
esac

cp -f ${top_srcdir}/data/sample.mgwport ${PACKAGE}-${VERSION}-1.${EXT}

${top_builddir}/bin/mgwport-inplace ${PACKAGE}-${VERSION}-1.${EXT} prep compile install pkg

cp -r ${PACKAGE}-${VERSION}-1/dist/${PACKAGE} .

${top_builddir}/bin/mgwport-inplace ${PACKAGE}-${VERSION}-1.${EXT} finish

rm -f ${PACKAGE}-${VERSION}-1-mingw32-src.tar.lzma \
  ${PACKAGE}-${VERSION}-1-mingw32-bin.tar.lzma \
  ${PACKAGE}-${VERSION}-1-mingw32-doc.tar.lzma \
  ${PACKAGE}-${VERSION}-1-mingw32-lic.tar.lzma \
  ${PACKAGE}-${VERSION}-1.mgwport \
  ${PACKAGE}-${VERSION}-1-msys-*-src.tar.lzma \
  ${PACKAGE}-${VERSION}-1-msys-*-bin.tar.lzma \
  ${PACKAGE}-${VERSION}-1-msys-*-doc.tar.lzma \
  ${PACKAGE}-${VERSION}-1-msys-*-lic.tar.lzma \
  ${PACKAGE}-${VERSION}-1.msysport

rm -f ${PACKAGE}-${VERSION}.tar.xz
